package ms.unicorn.language.languagedef;

import ms.unicorn.EPhantomAttribute;
import ms.unicorn.Element;
import ms.unicorn.Entity;
import ms.unicorn.UnicornPackage;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.EAttributeImpl;
import org.eclipse.emf.ecore.impl.ENotificationImpl;

public class EPhantomAttributeImpl extends EAttributeImpl implements EPhantomAttribute {

	private TagEntryDef fieldDef;
	private boolean visible;
	private String category;
	private String displayName;
	private String description;
	private boolean multiline;
	private boolean sortchoice;
	private Object image;
	
	public EPhantomAttributeImpl(TagEntryDef def, Class<?> containerClass, EClass containerEClass) {
		this.setFieldDef(def);
		
		setName(fieldDef.getName());
		setEType(TagDefUtil.getDataType(def));
		setTransient(true);
		
		EList<Parameter> params = def.getParameters();
		
		setChangeable(ParameterUtil.getEditable(params, true));
		
		
		setVisible(ParameterUtil.getVisible(params, true));
		setCategory(ParameterUtil.getParameter(params, ParameterType.CATEGORY, "Misc."));
		
		displayName = ParameterUtil.getParameter(params, ParameterType.TEXT, null);
		description = ParameterUtil.getParameter(params, ParameterType.DESCRIPTION, null);
		multiline = ParameterUtil.getParameter(params, ParameterType.MULTILINE, false);
		sortchoice = false; //ParameterUtil.getParameter(params, "sortchoice", false);
		
		
		setContainerClass(containerClass);
		//EClass eClass = UnicornPackage.eINSTANCE.getEntity();
		containerEClass.getEStructuralFeatures().add(this);
		setFeatureID(containerEClass.getEStructuralFeatures().size());
	}

	public TagEntryDef getFieldDef() {
		return fieldDef;
	}

	public void setFieldDef(TagEntryDef fieldDef) {
		this.fieldDef = fieldDef;
	}

	public <T> T getValue(Element e) {
		EDataType type = TagDefUtil.getDataType(fieldDef);
		return e.getProperty(type, fieldDef.getName());
	}
	
	public <T> void setValue(Element e, T value) {
		EDataType type = TagDefUtil.getDataType(fieldDef);
		Object oldValue = getValue(e);
		e.setProperty(type, fieldDef.getName(), value);
		e.eNotify(new ENotificationImpl((InternalEObject) e, Notification.SET, this, oldValue, getValue(e)));
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String group) {
		this.category = group;
	}

	@Override
	public String getDisplayName() {
		return this.displayName != null ? this.displayName : getName();
	}

	@Override
	public String getDescription() {
		return this.description;
	}

	@Override
	public boolean isMultiLine() {
		return this.multiline;
	}

	@Override
	public boolean isSortChoice() {
		return this.sortchoice;
	}

	@Override
	public Object getImage() {
		return this.image != null ? this.image : TagDefUtil.getDefaultImage(fieldDef);
	}

}
